// Comprehensive Option<T> type tests
// Tests all Option constructors and methods

func get_default() int {
    return 88
}

func require_even(value int) Option<int> {
    if value % 2 != 0 {
        return Option::None()
    }
    return Option::Some(value)
}

func option_try_sum(a int, b int) Option<int> {
    var first int = require_even(a)?
    var second int = require_even(b)?
    return Option::Some(first + second)
}

func safe_divide_option(a int, b int) Option<int> {
    if b == 0 {
        return Option::None()
    }
    return Option::Some(a / b)
}

func chained_division(a int, b int, c int) Option<int> {
    var first int = safe_divide_option(a, b)?
    var second int = safe_divide_option(first, c)?
    return Option::Some(second)
}

func describe_load(primary Option<int>, secondary Option<int>) Option<String> {
    var main_value int = primary?
    var secondary_value int = secondary?
    if main_value == 0 {
        return Option::None()
    }
    if secondary_value == 0 {
        return Option::None()
    }
    if main_value >= secondary_value {
        return Option::Some("primary")
    }
    return Option::Some("secondary")
}

func sum_nullable(a int?, b int?) Option<int> {
    var left int = a?
    var right int = b?
    return Option::Some(left + right)
}

func propagate_none_literal(should_fail bool) Option<int> {
    if should_fail {
        var auto_fail int = None?
        return Option::Some(auto_fail)
    }
    return Option::Some(256)
}

func lambda_none_pipeline(trigger bool) Option<int> {
    var worker func() Option<int> = func() Option<int> {
        var intermediate int = None?
        return Option::Some(intermediate)
    }

    if trigger {
        return worker()
    }
    return Option::Some(512)
}


func test_option_try() {
    println("\n[Test 105] ? operator success path")
    var opt_try_success Option<int> = option_try_sum(4, 8)
    assert(opt_try_success.is_some(), "? should keep value when all inputs are Some")
    assert(
        opt_try_success.unwrap() == 12,
        "unwrap() after ? should return the computed sum"
    )
    println("✓ ? operator unwraps Option::Some values")

    // Test 106: ? operator propagates None
    println("\n[Test 106] ? operator propagates None")
    var opt_try_fail Option<int> = option_try_sum(4, 7)
    assert(opt_try_fail.is_none(), "? should return None when any operand is None")
    println("✓ ? operator returns None when encountering a None operand")
}

func test_option_try_chain_divisions() {
    println("\n[Test 107] ? operator with chained divisions")
    var chained_success Option<int> = chained_division(40, 2, 2)
    assert(chained_success.is_some(), "Chained division should succeed for non-zero divisors")
    assert(chained_success.unwrap() == 10, "40 / 2 / 2 should equal 10")

    var first_failure Option<int> = chained_division(40, 0, 2)
    assert(first_failure.is_none(), "Division by zero should return None immediately")

    var second_failure Option<int> = chained_division(40, 2, 0)
    assert(second_failure.is_none(), "Second division by zero should also return None")
    println("✓ Chained divisions propagate None correctly via ?")
}

func test_option_try_string_descriptions() {
    println("\n[Test 108] ? operator composing Option<int> into Option<String>")
    var label Option<String> = describe_load(Option::Some(15), Option::Some(10))
    assert(label.is_some(), "Both data sources exist, so we get a label")
    assert(label.unwrap() == "primary", "Primary load should win when >= secondary")

    var missing_label Option<String> = describe_load(Option::Some(0), Option::Some(12))
    assert(missing_label.is_none(), "Zero load should propagate None")
    println("✓ String descriptions respect ? propagation")
}

func test_option_try_dart_style_values() {
    println("\n[Test 109] ? operator with Dart-style nullable ints")
    var left int? = 15
    var right int? = 5
    var sum Option<int> = sum_nullable(left, right)
    assert(sum.is_some(), "Both nullable ints have values")
    assert(sum.unwrap() == 20, "15 + 5 should equal 20")

    var none_value int? = None
    var missing_sum Option<int> = sum_nullable(left, none_value)
    assert(missing_sum.is_none(), "Missing operand should return None")
    println("✓ Dart-style nullable ints interact with ? as expected")
}

func test_option_if_var_pattern() {
    println("\n[Test 110] if var Some binding succeeds")
    var maybe Option<int> = Option::Some(64)
    var extracted int = -1
    if var Some(value) = maybe {
        extracted = value
    } else {
        extracted = -999
    }
    assert(extracted == 64, "if var should bind the inner Some value")
    println("✓ Bound Option::Some payload via if var")

    println("\n[Test 111] if var Some fallback hits else when None")
    var fallback int = 7
    var missing Option<int> = None
    if var Some(value) = missing {
        fallback = value
    } else {
        fallback = -100
    }
    assert(fallback == -100, "else branch should execute when Option is None")
    println("✓ else branch executes when pattern does not match")

    println("\n[Test 112] if var None pattern without else")
    var seen String = "unseen"
    var none_opt Option<int> = Option::None()
    if var None = none_opt {
        seen = "none"
    }
    assert(seen == "none", "Explicit None pattern should run even without else")
    println("✓ explicit None pattern executes without else branch")
}

func test_option_none_literal_inference() {
    println("\n[Test 113] None literal cooperates with `?` in Option-returning function")
    var literal_failure Option<int> = propagate_none_literal(true)
    assert(
        literal_failure.is_none(),
        "Invoking None? inside Option-returning function should propagate None"
    )
    var literal_success Option<int> = propagate_none_literal(false)
    assert(literal_success.is_some(), "Skipping the None? branch should keep the value")
    assert(
        literal_success.unwrap() == 256,
        "Success path should return the expected placeholder value"
    )
    println("✓ None literal + ? short-circuits only when executed")

    println("\n[Test 114] None literal cooperates with `?` inside lambda contexts")
    var lambda_failure Option<int> = lambda_none_pipeline(true)
    assert(lambda_failure.is_none(), "Lambda should propagate None literal when triggered")
    var lambda_success Option<int> = lambda_none_pipeline(false)
    assert(lambda_success.is_some(), "Not invoking the lambda should keep fallback Some")
    assert(
        lambda_success.unwrap() == 512,
        "Fallback path should preserve the configured value"
    )
    println("✓ Lambdas inherit the surrounding try-context for None inference")
}

// Struct/enum specific tests live in examples/option_struct_enum_test.nr

func test_option_suite() {
    println("=== Option Type Comprehensive Tests ===")

    // Test 1: Option::Some construction
    println("\n[Test 1] Option::Some construction")
    var opt_some Option<int> = Option::Some(42)
    assert(opt_some.is_some(), "Option::Some(42) should be Some")
    assert(!opt_some.is_none(), "Option::Some(42) should not be None")
    println("✓ Option::Some construction works")

    // Test 2: Option::None construction
    println("\n[Test 2] Option::None construction")
    var opt_none Option<int> = Option::None()
    assert(!opt_none.is_some(), "Option::None() should not be Some")
    assert(opt_none.is_none(), "Option::None() should be None")
    println("✓ Option::None construction works")

    // Test 3: is_some() method
    println("\n[Test 3] is_some() method")
    var opt1 Option<int> = Option::Some(100)
    var opt2 Option<int> = Option::None()
    assert(opt1.is_some(), "Some value should return true for is_some()")
    assert(!opt2.is_some(), "None should return false for is_some()")
    println("✓ is_some() method works correctly")

    // Test 4: is_none() method
    println("\n[Test 4] is_none() method")
    assert(!opt1.is_none(), "Some value should return false for is_none()")
    assert(opt2.is_none(), "None should return true for is_none()")
    println("✓ is_none() method works correctly")

    // Test 5: unwrap() method on Some
    println("\n[Test 5] unwrap() on Some")
    var value1 int = opt1.unwrap()
    assert(value1 == 100, "unwrap() should return the wrapped value")
    println("✓ unwrap() on Some works correctly")

    // Test 6: unwrap_or() method on Some
    println("\n[Test 6] unwrap_or() on Some")
    var value2 int = opt1.unwrap_or(999)
    assert(value2 == 100, "unwrap_or() on Some should return the wrapped value")
    println("✓ unwrap_or() on Some works correctly")

    // Test 7: unwrap_or() method on None
    println("\n[Test 7] unwrap_or() on None")
    var value3 int = opt2.unwrap_or(999)
    assert(value3 == 999, "unwrap_or() on None should return the default value")
    println("✓ unwrap_or() on None works correctly")

    // Test 8: Different types - Option<bool>
    println("\n[Test 8] Option<bool>")
    var opt_bool_some Option<bool> = Option::Some(true)
    var opt_bool_none Option<bool> = Option::None()
    assert(opt_bool_some.is_some(), "Option::Some(true) should be Some")
    assert(opt_bool_none.is_none(), "Option::None() for bool should be None")
    var bool_val bool = opt_bool_some.unwrap()
    assert(bool_val, "unwrap() should return true")
    var bool_default bool = opt_bool_none.unwrap_or(false)
    assert(!bool_default, "unwrap_or(false) on None should return false")
    println("✓ Option<bool> works correctly")

    // Test 9: Chaining - multiple operations
    println("\n[Test 9] Chaining operations")
    var opt_chain1 Option<int> = Option::Some(50)
    var result1 int = opt_chain1.unwrap_or(0)
    assert(result1 == 50, "Chain: unwrap_or should return 50")

    var opt_chain2 Option<int> = Option::None()
    var result2 int = opt_chain2.unwrap_or(0)
    assert(result2 == 0, "Chain: unwrap_or on None should return 0")
    println("✓ Chaining operations work correctly")

    // Test 10: Edge cases - zero values
    println("\n[Test 10] Edge cases - zero values")
    var opt_zero Option<int> = Option::Some(0)
    assert(opt_zero.is_some(), "Option::Some(0) should be Some")
    var zero_val int = opt_zero.unwrap()
    assert(zero_val == 0, "unwrap() should return 0")
    println("✓ Zero value handling works correctly")

    // Test 11: Multiple variables
    println("\n[Test 11] Multiple Option variables")
    var opt_a Option<int> = Option::Some(1)
    var opt_b Option<int> = Option::Some(2)
    var opt_c Option<int> = Option::None()
    assert(opt_a.is_some(), "opt_a should be Some")
    assert(opt_b.is_some(), "opt_b should be Some")
    assert(opt_c.is_none(), "Multiple variables with None")
    var sum int = opt_a.unwrap() + opt_b.unwrap() + opt_c.unwrap_or(0)
    assert(sum == 3, "Sum of unwrapped values should be 3")
    println("✓ Multiple Option variables work correctly")

    // Test 12: Reassignment
    println("\n[Test 12] Option reassignment")
    var opt_reassign Option<int> = Option::Some(10)
    assert(opt_reassign.unwrap() == 10, "Initial value should be 10")
    opt_reassign = Option::Some(20)
    assert(opt_reassign.unwrap() == 20, "Reassigned value should be 20")
    opt_reassign = Option::None()
    assert(opt_reassign.is_none(), "Reassigned to None should be None")
    println("✓ Option reassignment works correctly")

    // Test 13: Large numbers
    println("\n[Test 13] Large numbers")
    var opt_large Option<int> = Option::Some(1000000)
    var large_val int = opt_large.unwrap()
    assert(large_val == 1000000, "unwrap() should return 1000000")
    println("✓ Large number handling works correctly")

    // Test 14: Conditional logic with positive values
    println("\n[Test 14] Conditional logic with Option")
    var opt_cond Option<int> = Option::Some(5)
    var result_cond int = 0
    if opt_cond.is_some() {
        result_cond = opt_cond.unwrap() * 2
    } else {
        result_cond = 999
    }
    assert(result_cond == 10, "Conditional with Some should compute 5 * 2 = 10")

    var opt_cond2 Option<int> = Option::None()
    var result_cond2 int = 0
    if opt_cond2.is_some() {
        result_cond2 = opt_cond2.unwrap() * 2
    } else {
        result_cond2 = 888
    }
    assert(result_cond2 == 888, "Conditional with None should return 888")
    println("✓ Conditional logic with Option works correctly")

    // Test 15: expect() method with Some
    println("\n[Test 15] expect() method on Some")
    var opt_expect Option<int> = Option::Some(77)
    var val_expect int = opt_expect.expect("Should not panic")
    assert(val_expect == 77, "expect() should return 77")
    println("✓ expect() on Some works correctly")

    // Test 16: unwrap_or_else() on Some
    println("\n[Test 16] unwrap_or_else() on Some")
    var opt_some_else Option<int> = Option::Some(99)
    var val_some_else int = opt_some_else.unwrap_or_else(get_default)
    assert(val_some_else == 99, "unwrap_or_else on Some should return 99")
    println("✓ unwrap_or_else() on Some works correctly")

    // Test 17: unwrap_or_else() on None
    println("\n[Test 17] unwrap_or_else() on None")
    var opt_none_else Option<int> = Option::None()
    var val_none_else int = opt_none_else.unwrap_or_else(get_default)
    assert(val_none_else == 88, "unwrap_or_else on None should return 88")
    println("✓ unwrap_or_else() on None works correctly")

    // Test 18: Negative numbers - basic
    println("\n[Test 18] Negative numbers - basic")
    var opt_neg1 Option<int> = Option::Some(-1)
    assert(opt_neg1.is_some(), "Option::Some(-1) should be Some")
    var val_neg1 int = opt_neg1.unwrap()
    assert(val_neg1 == -1, "unwrap() should return -1")
    println("✓ Negative number -1 works correctly")

    // Test 19: Negative numbers - -42
    println("\n[Test 19] Negative numbers - -42")
    var opt_neg42 Option<int> = Option::Some(-42)
    var val_neg42 int = opt_neg42.unwrap()
    assert(val_neg42 == -42, "unwrap() should return -42")
    println("✓ Negative number -42 works correctly")

    // Test 20: Negative numbers - large
    println("\n[Test 20] Negative numbers - large")
    var opt_neg_large Option<int> = Option::Some(-1000000)
    var val_neg_large int = opt_neg_large.unwrap()
    assert(val_neg_large == -1000000, "unwrap() should return -1000000")
    println("✓ Large negative number -1000000 works correctly")

    // Test 21: Negative numbers with unwrap_or
    println("\n[Test 21] Negative numbers with unwrap_or")
    var opt_neg_or Option<int> = Option::Some(-99)
    var val_neg_or int = opt_neg_or.unwrap_or(0)
    assert(val_neg_or == -99, "unwrap_or on Some(-99) should return -99")

    var opt_none_neg Option<int> = Option::None()
    var val_none_neg int = opt_none_neg.unwrap_or(-999)
    assert(val_none_neg == -999, "unwrap_or on None should return -999")
    println("✓ Negative numbers with unwrap_or work correctly")

    // Test 22: Mixed positive and negative
    println("\n[Test 22] Mixed positive and negative")
    var opt_pos Option<int> = Option::Some(100)
    var opt_neg Option<int> = Option::Some(-100)
    var sum_mixed int = opt_pos.unwrap() + opt_neg.unwrap()
    assert(sum_mixed == 0, "100 + (-100) should equal 0")
    println("✓ Mixed positive and negative numbers work correctly")

    // Test 23: map() method on Some
    println("\n[Test 23] map() method on Some")
    var opt_map Option<int> = Option::Some(5)
    var opt_mapped Option<int> = opt_map.map(func(x int) int { return x * 2 })
    assert(opt_mapped.is_some(), "map() should preserve Some variant")
    var mapped_val int = opt_mapped.unwrap()
    assert(mapped_val == 10, "map() should transform 5 -> 10")
    println("✓ map() on Some works correctly (5 * 2 = 10)")

    // Test 24: map() method on None
    println("\n[Test 24] map() method on None")
    var opt_map_none Option<int> = Option::None()
    var opt_mapped_none Option<int> = opt_map_none.map(func(x int) int { return x * 2 })
    assert(opt_mapped_none.is_none(), "map() should preserve None variant")
    println("✓ map() on None works correctly")

    // Test 25: map() with negative numbers
    println("\n[Test 25] map() with negative numbers")
    var opt_map_neg Option<int> = Option::Some(-10)
    var opt_mapped_neg Option<int> = opt_map_neg.map(func(x int) int { return x * -3 })
    assert(opt_mapped_neg.is_some(), "map() should work with negative numbers")
    var mapped_neg_val int = opt_mapped_neg.unwrap()
    assert(mapped_neg_val == 30, "map() should transform -10 * -3 = 30")
    println("✓ map() with negative numbers works correctly (-10 * -3 = 30)")

    // Test 26: map() chaining multiple transformations
    println("\n[Test 26] map() chaining transformations")
    var opt_chain_map Option<int> = Option::Some(3)
    var opt_map_chain1 Option<int> = opt_chain_map.map(func(x int) int { return x + 2 })
    var opt_map_chain2 Option<int> = opt_map_chain1.map(func(x int) int { return x * 4 })
    assert(opt_map_chain2.is_some(), "Chained map() should preserve Some")
    var chain_val int = opt_map_chain2.unwrap()
    assert(chain_val == 20, "Chained map() should compute (3 + 2) * 4 = 20")
    println("✓ map() chaining works correctly ((3 + 2) * 4 = 20)")

    // Test 27: Option<int> with complex map transformation
    println("\n[Test 27] Option<int> with complex map transformation")
    var opt_complex Option<int> = Option::Some(7)
    var opt_complex_mapped Option<int> = opt_complex.map(func(x int) int {
        var result int = x * 2
        result = result + 3
        return result
    })
    assert(opt_complex_mapped.is_some(), "map() should work with complex transformations")
    var complex_mapped_val int = opt_complex_mapped.unwrap()
    assert(complex_mapped_val == 17, "map() should compute (7 * 2) + 3 = 17")
    println("✓ Option with complex map transformation works correctly")

    // ========================================
    // SECTION: Syntactic Sugar Tests (T?)
    // ========================================

    // Test 28: int? syntax (sugar for Option<int>)
    println("\n[Test 28] int? syntax (sugar for Option<int>)")
    var opt_sugar1 int? = Option::Some(42)
    assert(opt_sugar1.is_some(), "int? should work as Option<int>")
    var val_sugar1 int = opt_sugar1.unwrap()
    assert(val_sugar1 == 42, "unwrap() should return 42")
    println("✓ int? syntax works correctly")

    // Test 29: int? with None
    println("\n[Test 29] int? with None")
    var opt_sugar_none int? = Option::None()
    assert(opt_sugar_none.is_none(), "int? None should work")
    var default_sugar int = opt_sugar_none.unwrap_or(999)
    assert(default_sugar == 999, "unwrap_or should return 999")
    println("✓ int? None syntax works correctly")

    // Test 30: bool? syntax
    println("\n[Test 30] bool? syntax")
    var opt_bool_sugar bool? = Option::Some(true)
    assert(opt_bool_sugar.is_some(), "bool? should work")
    var bool_sugar_val bool = opt_bool_sugar.unwrap()
    assert(bool_sugar_val, "unwrap() should return true")
    println("✓ bool? syntax works correctly")

    // Test 31: Negative numbers with int? syntax
    println("\n[Test 31] Negative numbers with int? syntax")
    var opt_sugar_neg int? = Option::Some(-42)
    assert(opt_sugar_neg.is_some(), "int? with negative should work")
    var neg_sugar_val int = opt_sugar_neg.unwrap()
    assert(neg_sugar_val == -42, "unwrap() should return -42")
    println("✓ Negative numbers with int? work correctly")

    // Test 32: map() with int? syntax
    println("\n[Test 32] map() with int? syntax")
    var opt_sugar_map int? = Option::Some(5)
    var opt_sugar_mapped int? = opt_sugar_map.map(func(x int) int { return x * 2 })
    assert(opt_sugar_mapped.is_some(), "map() should work with int?")
    var mapped_sugar_val int = opt_sugar_mapped.unwrap()
    assert(mapped_sugar_val == 10, "map() should transform 5 -> 10")
    println("✓ map() with int? syntax works correctly")

    // Test 33: Chaining with int? syntax
    println("\n[Test 33] Chaining with int? syntax")
    var opt_sugar_chain int? = Option::Some(3)
    var opt_sugar_chain1 int? = opt_sugar_chain.map(func(x int) int { return x + 2 })
    var opt_sugar_chain2 int? = opt_sugar_chain1.map(func(x int) int { return x * x })
    assert(opt_sugar_chain2.is_some(), "Chained map should work with int?")
    var chain_sugar_val int = opt_sugar_chain2.unwrap()
    assert(chain_sugar_val == 25, "(3 + 2)^2 should equal 25")
    println("✓ Chaining with int? syntax works correctly")

    // ========================================
    // SECTION: Sweeter Sugar (Some/None without prefix)
    // ========================================

    // Test 34: Some without Option:: prefix
    println("\n[Test 34] Some(value) without Option:: prefix")
    var opt_sweet1 int? = Some(42)
    assert(opt_sweet1.is_some(), "Some(42) should work")
    var sweet_val1 int = opt_sweet1.unwrap()
    assert(sweet_val1 == 42, "unwrap() should return 42")
    println("✓ Some(value) works without Option:: prefix")

    // Test 35: None without Option:: prefix
    println("\n[Test 35] None without Option:: prefix")
    var opt_sweet_none int? = None
    assert(opt_sweet_none.is_none(), "None should work")
    var sweet_default int = opt_sweet_none.unwrap_or(999)
    assert(sweet_default == 999, "unwrap_or should return 999")
    println("✓ None works without Option:: prefix")

    // Test 36: Some with negative numbers
    println("\n[Test 36] Some(-42) without prefix")
    var opt_sweet_neg int? = Some(-42)
    assert(opt_sweet_neg.is_some(), "Some(-42) should work")
    var sweet_neg_val int = opt_sweet_neg.unwrap()
    assert(sweet_neg_val == -42, "unwrap() should return -42")
    println("✓ Some(-42) works correctly")

    // Test 37: map() with Some (no prefix)
    println("\n[Test 37] map() with Some (no prefix)")
    var opt_sweet_map int? = Some(5)
    var opt_sweet_mapped int? = opt_sweet_map.map(func(x int) int { return x * 2 })
    assert(opt_sweet_mapped.is_some(), "map() should work with Some")
    var sweet_mapped_val int = opt_sweet_mapped.unwrap()
    assert(sweet_mapped_val == 10, "map() should transform 5 -> 10")
    println("✓ map() with Some works correctly")

    // Test 38: Comprehensive Some/None usage in function returns
    println("\n[Test 38] Function returning Some/None")
    var test_found int? = Some(100)
    assert(test_found.is_some(), "Function should return Some")
    assert(test_found.unwrap() == 100, "Should unwrap to 100")

    var test_not_found int? = None
    assert(test_not_found.is_none(), "Function should return None")
    assert(test_not_found.unwrap_or(-1) == -1, "None.unwrap_or(-1) should be -1")
    println("✓ Function returns with Some/None work correctly")

    // Test 39: Chaining with Some (no prefix)
    println("\n[Test 39] Chaining with Some (no prefix)")
    var test_chain_start int? = Some(3)
    var test_chain_step1 int? = test_chain_start.map(func(x int) int { return x + 2 })
    var test_chain_step2 int? = test_chain_step1.map(func(x int) int { return x * x })
    assert(test_chain_step2.is_some(), "Chaining should preserve Some")
    assert(test_chain_step2.unwrap() == 25, "Should compute (3+2)^2 = 25")
    println("✓ Chaining with Some works correctly")

    // Test 40: None in various contexts
    println("\n[Test 40] None in various contexts")
    var none1 int? = None
    var none2 int? = None
    assert(none1.is_none(), "none1 should be None")
    assert(none2.is_none(), "none2 should be None")
    assert(none1.unwrap_or(0) == 0, "None.unwrap_or(0) should be 0")
    assert(none2.unwrap_or(999) == 999, "None.unwrap_or(999) should be 999")
    println("✓ None in various contexts works correctly")

    // Test 41: Mixed Some and None operations
    println("\n[Test 41] Mixed Some and None operations")
    var mixed_a int? = Some(10)
    var mixed_b int? = None
    var mixed_c int? = Some(20)

    assert(mixed_a.is_some(), "mixed_a should be Some")
    assert(mixed_b.is_none(), "mixed_b should be None")
    assert(mixed_c.is_some(), "mixed_c should be Some")

    var test_sum int = mixed_a.unwrap_or(0) + mixed_b.unwrap_or(0) + mixed_c.unwrap_or(0)
    assert(test_sum == 30, "10 + 0 + 20 should equal 30")
    println("✓ Mixed Some and None operations work correctly")

    // Test 42: Large values with Some (no prefix)
    println("\n[Test 42] Large values with Some")
    var test_large int? = Some(1000000)
    assert(test_large.is_some(), "Large value should be Some")
    assert(test_large.unwrap() == 1000000, "Should unwrap to 1000000")
    println("✓ Large values with Some work correctly")

    // Test 43: bool type with Some/None
    println("\n[Test 43] bool type with Some/None")
    var bool_some bool? = Some(true)
    var bool_none bool? = None

    assert(bool_some.is_some(), "bool Some should be Some")
    assert(bool_some.unwrap(), "Should unwrap to true")
    assert(bool_none.is_none(), "bool None should be None")
    assert(!bool_none.unwrap_or(false), "None.unwrap_or(false) should be false")
    println("✓ bool type with Some/None works correctly")

    // ========================================
    // SECTION: Advanced Methods Tests
    // ========================================

    // Test 44: and_then() with Some
    println("\n[Test 44] and_then() with Some")
    var opt_and_then Option<int> = Option::Some(5)
    var opt_chained Option<int> = opt_and_then.and_then(func(x int) Option<int> {
        return Option::Some(x * 2)
    })
    assert(opt_chained.is_some(), "and_then() should return Some")
    assert(opt_chained.unwrap() == 10, "and_then() should compute 5 * 2 = 10")
    println("✓ and_then() with Some works correctly")

    // Test 45: and_then() with None
    println("\n[Test 45] and_then() with None")
    var opt_and_then_none Option<int> = Option::None()
    var opt_chained_none Option<int> = opt_and_then_none.and_then(func(x int) Option<int> {
        return Option::Some(x * 2)
    })
    assert(opt_chained_none.is_none(), "and_then() should preserve None")
    println("✓ and_then() with None works correctly")

    // Test 46: or_else() with None
    println("\n[Test 46] or_else() with None")
    var opt_or_else_none Option<int> = Option::None()
    var opt_recovered Option<int> = opt_or_else_none.or_else(func() Option<int> {
        return Option::Some(99)
    })
    assert(opt_recovered.is_some(), "or_else() should recover from None")
    assert(opt_recovered.unwrap() == 99, "or_else() should provide fallback 99")
    println("✓ or_else() with None works correctly")

    // Test 47: or_else() with Some
    println("\n[Test 47] or_else() with Some")
    var opt_or_else_some Option<int> = Option::Some(42)
    var opt_not_recovered Option<int> = opt_or_else_some.or_else(func() Option<int> {
        return Option::Some(99)
    })
    assert(opt_not_recovered.is_some(), "or_else() should preserve Some")
    assert(opt_not_recovered.unwrap() == 42, "or_else() should not change Some value")
    println("✓ or_else() with Some works correctly")

    // Test 48: filter() with predicate true
    println("\n[Test 48] filter() with predicate true")
    var opt_filter Option<int> = Option::Some(10)
    var opt_filtered Option<int> = opt_filter.filter(func(x int) bool {
        return x > 5
    })
    assert(opt_filtered.is_some(), "filter() should keep Some when predicate is true")
    assert(opt_filtered.unwrap() == 10, "filter() should preserve value when predicate is true")
    println("✓ filter() with predicate true works correctly")

    // Test 49: filter() with predicate false
    println("\n[Test 49] filter() with predicate false")
    var opt_filter2 Option<int> = Option::Some(3)
    var opt_filtered2 Option<int> = opt_filter2.filter(func(x int) bool {
        return x > 5
    })
    assert(opt_filtered2.is_none(), "filter() should convert to None when predicate is false")
    println("✓ filter() with predicate false works correctly")

    // Test 50: filter() with None
    println("\n[Test 50] filter() with None")
    var opt_filter_none Option<int> = Option::None()
    var opt_filtered_none Option<int> = opt_filter_none.filter(func(x int) bool {
        return x > 5
    })
    assert(opt_filtered_none.is_none(), "filter() should preserve None")
    println("✓ filter() with None works correctly")

    // Test 51: and() with first Some, second Some
    println("\n[Test 51] and() with first Some, second Some")
    var opt_and1 Option<int> = Option::Some(10)
    var opt_and2 Option<String> = Option::Some("hello")
    var opt_and_result Option<String> = opt_and1.and(opt_and2)
    assert(opt_and_result.is_some(), "and() should return second Some when both are Some")
    assert(opt_and_result.unwrap() == "hello", "and() should return second Option value")
    println("✓ and() with both Some works correctly")

    // Test 52: and() with first None
    println("\n[Test 52] and() with first None")
    var opt_and_none1 Option<int> = Option::None()
    var opt_and_some Option<String> = Option::Some("world")
    var opt_and_result2 Option<String> = opt_and_none1.and(opt_and_some)
    assert(opt_and_result2.is_none(), "and() should return None when first is None")
    println("✓ and() with first None works correctly")

    // Test 53: or() with first Some
    println("\n[Test 53] or() with first Some")
    var opt_or1 Option<int> = Option::Some(100)
    var opt_or2 Option<int> = Option::Some(200)
    var opt_or_result Option<int> = opt_or1.or(opt_or2)
    assert(opt_or_result.is_some(), "or() should return first Some when first is Some")
    assert(opt_or_result.unwrap() == 100, "or() should return first Option value")
    println("✓ or() with first Some works correctly")

    // Test 54: or() with first None, second Some
    println("\n[Test 54] or() with first None, second Some")
    var opt_or_none Option<int> = Option::None()
    var opt_or_some Option<int> = Option::Some(300)
    var opt_or_result2 Option<int> = opt_or_none.or(opt_or_some)
    assert(opt_or_result2.is_some(), "or() should return second Some when first is None")
    assert(opt_or_result2.unwrap() == 300, "or() should return second Option value")
    println("✓ or() with first None, second Some works correctly")

    // Test 55: or() with both None
    println("\n[Test 55] or() with both None")
    var opt_or_none1 Option<int> = Option::None()
    var opt_or_none2 Option<int> = Option::None()
    var opt_or_result3 Option<int> = opt_or_none1.or(opt_or_none2)
    assert(opt_or_result3.is_none(), "or() should return None when both are None")
    println("✓ or() with both None works correctly")

    // Test 56: xor() with first Some, second None
    println("\n[Test 56] xor() with first Some, second None")
    var opt_xor1 Option<int> = Option::Some(50)
    var opt_xor2 Option<int> = Option::None()
    var opt_xor_result Option<int> = opt_xor1.xor(opt_xor2)
    assert(opt_xor_result.is_some(), "xor() should return Some when exactly one is Some")
    assert(opt_xor_result.unwrap() == 50, "xor() should return first Some value")
    println("✓ xor() with first Some, second None works correctly")

    // Test 57: xor() with first None, second Some
    println("\n[Test 57] xor() with first None, second Some")
    var opt_xor_none Option<int> = Option::None()
    var opt_xor_some Option<int> = Option::Some(60)
    var opt_xor_result2 Option<int> = opt_xor_none.xor(opt_xor_some)
    assert(opt_xor_result2.is_some(), "xor() should return Some when exactly one is Some")
    assert(opt_xor_result2.unwrap() == 60, "xor() should return second Some value")
    println("✓ xor() with first None, second Some works correctly")

    // Test 58: xor() with both Some
    println("\n[Test 58] xor() with both Some")
    var opt_xor_both1 Option<int> = Option::Some(70)
    var opt_xor_both2 Option<int> = Option::Some(80)
    var opt_xor_result3 Option<int> = opt_xor_both1.xor(opt_xor_both2)
    assert(opt_xor_result3.is_none(), "xor() should return None when both are Some")
    println("✓ xor() with both Some works correctly")

    // Test 59: xor() with both None
    println("\n[Test 59] xor() with both None")
    var opt_xor_none1 Option<int> = Option::None()
    var opt_xor_none2 Option<int> = Option::None()
    var opt_xor_result4 Option<int> = opt_xor_none1.xor(opt_xor_none2)
    assert(opt_xor_result4.is_none(), "xor() should return None when both are None")
    println("✓ xor() with both None works correctly")

    // Test 60: ok_or() with Some
    println("\n[Test 60] ok_or() with Some")
    var opt_ok_or Option<int> = Option::Some(42)
    var result_ok_or Result<int, String> = opt_ok_or.ok_or("error message")
    assert(result_ok_or.is_ok(), "ok_or() should return Ok when Some")
    assert(result_ok_or.unwrap() == 42, "ok_or() should preserve Some value")
    println("✓ ok_or() with Some works correctly")

    // Test 61: ok_or() with None
    println("\n[Test 61] ok_or() with None")
    var opt_ok_or_none Option<int> = Option::None()
    var result_ok_or_err Result<int, String> = opt_ok_or_none.ok_or("default error")
    assert(result_ok_or_err.is_err(), "ok_or() should return Err when None")
    assert(result_ok_or_err.unwrap_err() == "default error", "ok_or() should use provided error")
    println("✓ ok_or() with None works correctly")

    // Test 62: ok_or_else() with Some
    println("\n[Test 62] ok_or_else() with Some")
    var opt_ok_or_else Option<int> = Option::Some(88)
    var result_ok_or_else Result<int, String> = opt_ok_or_else.ok_or_else(func() String {
        return "computed error"
    })
    assert(result_ok_or_else.is_ok(), "ok_or_else() should return Ok when Some")
    assert(result_ok_or_else.unwrap() == 88, "ok_or_else() should preserve Some value")
    println("✓ ok_or_else() with Some works correctly")

    // Test 63: ok_or_else() with None
    println("\n[Test 63] ok_or_else() with None")
    var opt_ok_or_else_none Option<int> = Option::None()
    var result_ok_or_else_err Result<int, String> = opt_ok_or_else_none.ok_or_else(func() String {
        return "computed error message"
    })
    assert(result_ok_or_else_err.is_err(), "ok_or_else() should return Err when None")
    assert(result_ok_or_else_err.unwrap_err() == "computed error message", "ok_or_else() should compute error")
    println("✓ ok_or_else() with None works correctly")

    // Test 64: map_or() with Some
    println("\n[Test 64] map_or() with Some")
    var opt_map_or Option<int> = Option::Some(5)
    var map_or_val int = opt_map_or.map_or(999, func(x int) int {
        return x * 2
    })
    assert(map_or_val == 10, "map_or() should apply function when Some (5 * 2 = 10)")
    println("✓ map_or() with Some works correctly")

    // Test 65: map_or() with None
    println("\n[Test 65] map_or() with None")
    var opt_map_or_none Option<int> = Option::None()
    var map_or_default int = opt_map_or_none.map_or(999, func(x int) int {
        return x * 2
    })
    assert(map_or_default == 999, "map_or() should return default when None")
    println("✓ map_or() with None works correctly")

    // Test 66: map_or_else() with Some
    println("\n[Test 66] map_or_else() with Some")
    var opt_map_or_else Option<int> = Option::Some(7)
    var map_or_else_val int = opt_map_or_else.map_or_else(func() int {
        return 888
    }, func(x int) int {
        return x * 3
    })
    assert(map_or_else_val == 21, "map_or_else() should apply function when Some (7 * 3 = 21)")
    println("✓ map_or_else() with Some works correctly")

    // Test 67: map_or_else() with None
    println("\n[Test 67] map_or_else() with None")
    var opt_map_or_else_none Option<int> = Option::None()
    var map_or_else_default int = opt_map_or_else_none.map_or_else(func() int {
        return 777
    }, func(x int) int {
        return x * 3
    })
    assert(map_or_else_default == 777, "map_or_else() should compute default when None")
    println("✓ map_or_else() with None works correctly")

    // Test 68: Chaining and_then with filter
    println("\n[Test 68] Chaining and_then with filter")
    var opt_chain_advanced Option<int> = Option::Some(8)
    var opt_chain_filtered_68 Option<int> = opt_chain_advanced.filter(func(x int) bool {
        return x > 5
    })
    var opt_chain_final_68 Option<int> = opt_chain_filtered_68.and_then(func(x int) Option<int> {
        return Option::Some(x * x)
    })
    assert(opt_chain_final_68.is_some(), "Chained filter and and_then should work")
    assert(opt_chain_final_68.unwrap() == 64, "Should compute 8^2 = 64")
    println("✓ Chaining and_then with filter works correctly")

    // Test 69: Complex chain with map, filter, and_then
    println("\n[Test 69] Complex chain with map, filter, and_then")
    var opt_chain_test Option<int> = Option::Some(6)
    var opt_chain_mapped_69 Option<int> = opt_chain_test.map(func(x int) int { return x + 4 })
    var opt_chain_filtered_69 Option<int> = opt_chain_mapped_69.filter(func(x int) bool { return x > 5 })
    var opt_chain_final_69 Option<int> = opt_chain_filtered_69.and_then(func(x int) Option<int> {
        return Option::Some(x * 2)
    })
    assert(opt_chain_final_69.is_some(), "Complex chain should work")
    assert(opt_chain_final_69.unwrap() == 20, "Should compute (6+4)*2 = 20")
    println("✓ Complex chain works correctly")

    // Test 70: ok_or with different error types
    println("\n[Test 70] ok_or with different error types")
    var opt_ok_or_int Option<int> = Option::Some(100)
    var result_ok_or_int Result<int, int> = opt_ok_or_int.ok_or(404)
    assert(result_ok_or_int.is_ok(), "ok_or with int error should work")
    assert(result_ok_or_int.unwrap() == 100, "Should preserve value")
    var opt_ok_or_int_none Option<int> = Option::None()
    var result_ok_or_int_err Result<int, int> = opt_ok_or_int_none.ok_or(500)
    assert(result_ok_or_int_err.is_err(), "ok_or with int error should work for None")
    assert(result_ok_or_int_err.unwrap_err() == 500, "Should use provided error code")
    println("✓ ok_or with different error types works correctly")

    // ========================================
    // SECTION: Dart-Style Nullable Type Syntax (int? a = 10)
    // ========================================

    // Test 71: int? with direct value assignment (Dart-style)
    println("\n[Test 71] Dart-style: int? a = 10")
    var dart_opt1 int? = 10
    assert(dart_opt1.is_some(), "int? a = 10 should auto-wrap to Some(10)")
    assert(!dart_opt1.is_none(), "int? a = 10 should not be None")
    var dart_val1 int = dart_opt1.unwrap()
    assert(dart_val1 == 10, "unwrap() should return 10")
    assert(dart_opt1.unwrap_or(999) == 10, "unwrap_or should return 10")
    println("✓ Dart-style int? a = 10 works correctly")

    // Test 72: int? with None (should not auto-wrap)
    println("\n[Test 72] Dart-style: int? b = None")
    var dart_opt2 int? = None
    assert(dart_opt2.is_none(), "int? b = None should be None")
    assert(!dart_opt2.is_some(), "int? b = None should not be Some")
    var dart_default int = dart_opt2.unwrap_or(888)
    assert(dart_default == 888, "unwrap_or should return 888")
    assert(dart_opt2.unwrap_or_else(get_default) == 88, "unwrap_or_else should return 88")
    println("✓ Dart-style int? b = None works correctly")

    // Test 73: int? with Some (should not double-wrap)
    println("\n[Test 73] Dart-style: int? c = Some(20)")
    var dart_opt3 int? = Some(20)
    assert(dart_opt3.is_some(), "int? c = Some(20) should be Some")
    assert(!dart_opt3.is_none(), "int? c = Some(20) should not be None")
    var dart_val3 int = dart_opt3.unwrap()
    assert(dart_val3 == 20, "unwrap() should return 20")
    assert(dart_opt3.expect("Should not panic") == 20, "expect() should return 20")
    println("✓ Dart-style int? c = Some(20) works correctly")

    // Test 74: bool? with direct value assignment
    println("\n[Test 74] Dart-style: bool? d = true")
    var dart_bool1 bool? = true
    assert(dart_bool1.is_some(), "bool? d = true should auto-wrap to Some(true)")
    assert(!dart_bool1.is_none(), "bool? d = true should not be None")
    var dart_bool_val bool = dart_bool1.unwrap()
    assert(dart_bool_val, "unwrap() should return true")
    assert(dart_bool1.unwrap_or(false), "unwrap_or should return true")
    println("✓ Dart-style bool? d = true works correctly")

    // Test 75: bool? with false
    println("\n[Test 75] Dart-style: bool? e = false")
    var dart_bool2 bool? = false
    assert(dart_bool2.is_some(), "bool? e = false should auto-wrap to Some(false)")
    assert(!dart_bool2.is_none(), "bool? e = false should not be None")
    var dart_bool_val2 bool = dart_bool2.unwrap()
    assert(!dart_bool_val2, "unwrap() should return false")
    assert(!dart_bool2.unwrap_or(true), "unwrap_or should return false")
    println("✓ Dart-style bool? e = false works correctly")

    // Test 76: Option<int> with direct value (full form)
    println("\n[Test 76] Dart-style: Option<int> f = 30")
    var dart_opt4 Option<int> = 30
    assert(dart_opt4.is_some(), "Option<int> f = 30 should auto-wrap to Some(30)")
    assert(!dart_opt4.is_none(), "Option<int> f = 30 should not be None")
    var dart_val4 int = dart_opt4.unwrap()
    assert(dart_val4 == 30, "unwrap() should return 30")
    assert(dart_opt4.unwrap_or(0) == 30, "unwrap_or should return 30")
    println("✓ Dart-style Option<int> f = 30 works correctly")

    // Test 77: int? with negative number
    println("\n[Test 77] Dart-style: int? g = -42")
    var dart_opt5 int? = -42
    assert(dart_opt5.is_some(), "int? g = -42 should auto-wrap to Some(-42)")
    assert(!dart_opt5.is_none(), "int? g = -42 should not be None")
    var dart_val5 int = dart_opt5.unwrap()
    assert(dart_val5 == -42, "unwrap() should return -42")
    assert(dart_opt5.unwrap_or(0) == -42, "unwrap_or should return -42")
    println("✓ Dart-style int? g = -42 works correctly")

    // Test 78: int? with zero value
    println("\n[Test 78] Dart-style: int? h = 0")
    var dart_opt6 int? = 0
    assert(dart_opt6.is_some(), "int? h = 0 should auto-wrap to Some(0)")
    assert(!dart_opt6.is_none(), "int? h = 0 should not be None")
    var dart_val6 int = dart_opt6.unwrap()
    assert(dart_val6 == 0, "unwrap() should return 0")
    assert(dart_opt6.unwrap_or(999) == 0, "unwrap_or should return 0")
    println("✓ Dart-style int? h = 0 works correctly")

    // Test 79: int? with variable assignment
    println("\n[Test 79] Dart-style: int? i = variable")
    var source_value int = 100
    var dart_opt7 int? = source_value
    assert(dart_opt7.is_some(), "int? i = variable should auto-wrap to Some(100)")
    assert(!dart_opt7.is_none(), "int? i = variable should not be None")
    var dart_val7 int = dart_opt7.unwrap()
    assert(dart_val7 == 100, "unwrap() should return 100")
    assert(dart_val7 == source_value, "unwrap() should equal source value")
    assert(dart_opt7.unwrap_or(0) == 100, "unwrap_or should return 100")
    println("✓ Dart-style int? i = variable works correctly")

    // Test 80: int? with large number
    println("\n[Test 80] Dart-style: int? j = 1000000")
    var dart_opt8 int? = 1000000
    assert(dart_opt8.is_some(), "int? j = 1000000 should auto-wrap to Some(1000000)")
    assert(!dart_opt8.is_none(), "int? j = 1000000 should not be None")
    var dart_val8 int = dart_opt8.unwrap()
    assert(dart_val8 == 1000000, "unwrap() should return 1000000")
    assert(dart_opt8.expect("Should not panic") == 1000000, "expect() should return 1000000")
    println("✓ Dart-style int? j = 1000000 works correctly")

    // Test 81: int? with map() after auto-wrap
    println("\n[Test 81] Dart-style: int? k = 5, then map")
    var dart_opt9 int? = 5
    assert(dart_opt9.is_some(), "int? k = 5 should auto-wrap to Some(5)")
    var dart_mapped int? = dart_opt9.map(func(x int) int { return x * 2 })
    assert(dart_mapped.is_some(), "map() should preserve Some")
    var dart_mapped_val int = dart_mapped.unwrap()
    assert(dart_mapped_val == 10, "map() should transform 5 -> 10")
    assert(dart_mapped.unwrap_or(0) == 10, "unwrap_or should return 10")
    println("✓ Dart-style int? with map() works correctly")

    // Test 82: int? with and_then() after auto-wrap
    println("\n[Test 82] Dart-style: int? l = 8, then and_then")
    var dart_opt10 int? = 8
    assert(dart_opt10.is_some(), "int? l = 8 should auto-wrap to Some(8)")
    var dart_chained int? = dart_opt10.and_then(func(x int) Option<int> {
        return Option::Some(x * x)
    })
    assert(dart_chained.is_some(), "and_then() should return Some")
    var dart_chained_val int = dart_chained.unwrap()
    assert(dart_chained_val == 64, "and_then() should compute 8^2 = 64")
    assert(dart_chained.unwrap_or(0) == 64, "unwrap_or should return 64")
    println("✓ Dart-style int? with and_then() works correctly")

    // Test 83: int? with filter() after auto-wrap
    println("\n[Test 83] Dart-style: int? m = 15, then filter")
    var dart_opt11 int? = 15
    assert(dart_opt11.is_some(), "int? m = 15 should auto-wrap to Some(15)")
    var dart_filtered1 int? = dart_opt11.filter(func(x int) bool { return x > 10 })
    assert(dart_filtered1.is_some(), "filter() should keep Some when predicate is true")
    assert(dart_filtered1.unwrap() == 15, "filter() should preserve value")
    var dart_filtered2 int? = dart_opt11.filter(func(x int) bool { return x < 10 })
    assert(dart_filtered2.is_none(), "filter() should convert to None when predicate is false")
    println("✓ Dart-style int? with filter() works correctly")

    // Test 84: Multiple Dart-style assignments
    println("\n[Test 84] Dart-style: Multiple assignments")
    var dart_a int? = 1
    var dart_b int? = 2
    var dart_c int? = 3
    assert(dart_a.is_some(), "dart_a should be Some")
    assert(dart_b.is_some(), "dart_b should be Some")
    assert(dart_c.is_some(), "dart_c should be Some")
    assert(dart_a.unwrap() == 1, "dart_a should be 1")
    assert(dart_b.unwrap() == 2, "dart_b should be 2")
    assert(dart_c.unwrap() == 3, "dart_c should be 3")
    var dart_sum int = dart_a.unwrap() + dart_b.unwrap() + dart_c.unwrap()
    assert(dart_sum == 6, "Sum should be 6")
    println("✓ Multiple Dart-style assignments work correctly")

    // Test 85: Dart-style with None mixed
    println("\n[Test 85] Dart-style: Mixed Some and None")
    var dart_some1 int? = 10
    var dart_none1 int? = None
    var dart_some2 int? = 20
    assert(dart_some1.is_some(), "dart_some1 should be Some")
    assert(dart_none1.is_none(), "dart_none1 should be None")
    assert(dart_some2.is_some(), "dart_some2 should be Some")
    var dart_total int = dart_some1.unwrap_or(0) + dart_none1.unwrap_or(0) + dart_some2.unwrap_or(0)
    assert(dart_total == 30, "Total should be 30")
    assert(dart_some1.unwrap() == 10, "dart_some1 should be 10")
    assert(dart_some2.unwrap() == 20, "dart_some2 should be 20")
    println("✓ Dart-style with mixed Some and None works correctly")

    // Test 86: Dart-style reassignment
    println("\n[Test 86] Dart-style: Reassignment")
    var dart_reassign int? = 50
    assert(dart_reassign.is_some(), "Initial should be Some")
    assert(dart_reassign.unwrap() == 50, "Initial value should be 50")
    dart_reassign = 60
    assert(dart_reassign.is_some(), "After reassignment should be Some")
    assert(dart_reassign.unwrap() == 60, "After reassignment should be 60")
    dart_reassign = None
    assert(dart_reassign.is_none(), "After None reassignment should be None")
    assert(dart_reassign.unwrap_or(999) == 999, "unwrap_or should return 999")
    println("✓ Dart-style reassignment works correctly")

    // Test 87: Dart-style with complex expression
    println("\n[Test 87] Dart-style: Complex expression")
    var dart_expr int? = 2 + 3
    assert(dart_expr.is_some(), "int? = 2 + 3 should auto-wrap to Some(5)")
    assert(dart_expr.unwrap() == 5, "unwrap() should return 5")
    var temp_val int = dart_expr.unwrap() * 2
    var dart_expr2 int? = temp_val
    assert(dart_expr2.is_some(), "int? = temp_val should auto-wrap to Some(10)")
    assert(dart_expr2.unwrap() == 10, "unwrap() should return 10")
    assert(dart_expr2.unwrap_or(0) == 10, "unwrap_or should return 10")
    println("✓ Dart-style with complex expression works correctly")

    // ========================================
    // SECTION: Mutable Methods Tests (take, replace)
    // ========================================

    // Test 88: take() with Some value
    println("\n[Test 88] take() with Some value")
    var opt_take1 Option<int> = Option::Some(42)
    assert(opt_take1.is_some(), "Initial should be Some")
    assert(opt_take1.unwrap() == 42, "Initial value should be 42")
    var taken1 Option<int> = opt_take1.take()
    assert(taken1.is_some(), "take() should return Some(42)")
    assert(taken1.unwrap() == 42, "take() should return original value 42")
    assert(opt_take1.is_none(), "After take(), original should be None")
    assert(!opt_take1.is_some(), "After take(), original should not be Some")
    println("✓ take() with Some works correctly")

    // Test 89: take() with None value
    println("\n[Test 89] take() with None value")
    var opt_take2 Option<int> = Option::None()
    assert(opt_take2.is_none(), "Initial should be None")
    var taken2 Option<int> = opt_take2.take()
    assert(taken2.is_none(), "take() on None should return None")
    assert(opt_take2.is_none(), "After take() on None, should still be None")
    assert(!opt_take2.is_some(), "After take() on None, should not be Some")
    println("✓ take() with None works correctly")

    // Test 90: take() with different types
    println("\n[Test 90] take() with bool type")
    var opt_take_bool Option<bool> = Option::Some(true)
    assert(opt_take_bool.is_some(), "Initial bool should be Some")
    assert(opt_take_bool.unwrap(), "Initial value should be true")
    var taken_bool Option<bool> = opt_take_bool.take()
    assert(taken_bool.is_some(), "take() should return Some(true)")
    assert(taken_bool.unwrap(), "take() should return true")
    assert(opt_take_bool.is_none(), "After take(), should be None")
    println("✓ take() with bool type works correctly")

    // Test 91: take() with negative numbers
    println("\n[Test 91] take() with negative numbers")
    var opt_take_neg Option<int> = Option::Some(-100)
    assert(opt_take_neg.is_some(), "Initial should be Some")
    assert(opt_take_neg.unwrap() == -100, "Initial value should be -100")
    var taken_neg Option<int> = opt_take_neg.take()
    assert(taken_neg.is_some(), "take() should return Some(-100)")
    assert(taken_neg.unwrap() == -100, "take() should return -100")
    assert(opt_take_neg.is_none(), "After take(), should be None")
    println("✓ take() with negative numbers works correctly")

    // Test 92: take() multiple times
    println("\n[Test 92] take() multiple times")
    var opt_take_multi Option<int> = Option::Some(50)
    assert(opt_take_multi.is_some(), "First take: initial should be Some")
    var taken_multi1 Option<int> = opt_take_multi.take()
    assert(taken_multi1.is_some(), "First take() should return Some")
    assert(taken_multi1.unwrap() == 50, "First take() should return 50")
    assert(opt_take_multi.is_none(), "After first take(), should be None")
    var taken_multi2 Option<int> = opt_take_multi.take()
    assert(taken_multi2.is_none(), "Second take() on None should return None")
    assert(opt_take_multi.is_none(), "After second take(), should still be None")
    println("✓ take() multiple times works correctly")

    // Test 93: replace() with Some value
    println("\n[Test 93] replace() with Some value")
    var opt_replace1 Option<int> = Option::Some(10)
    assert(opt_replace1.is_some(), "Initial should be Some")
    assert(opt_replace1.unwrap() == 10, "Initial value should be 10")
    var replaced1 Option<int> = opt_replace1.replace(20)
    assert(replaced1.is_some(), "replace() should return Some(10)")
    assert(replaced1.unwrap() == 10, "replace() should return old value 10")
    assert(opt_replace1.is_some(), "After replace(), should be Some")
    assert(opt_replace1.unwrap() == 20, "After replace(), new value should be 20")
    println("✓ replace() with Some works correctly")

    // Test 94: replace() with None value
    println("\n[Test 94] replace() with None value")
    var opt_replace2 Option<int> = Option::None()
    assert(opt_replace2.is_none(), "Initial should be None")
    var replaced2 Option<int> = opt_replace2.replace(30)
    assert(replaced2.is_none(), "replace() on None should return None")
    assert(opt_replace2.is_some(), "After replace(), should be Some")
    assert(opt_replace2.unwrap() == 30, "After replace(), new value should be 30")
    println("✓ replace() with None works correctly")

    // Test 95: replace() with different types
    println("\n[Test 95] replace() with bool type")
    var opt_replace_bool Option<bool> = Option::Some(false)
    assert(opt_replace_bool.is_some(), "Initial bool should be Some")
    assert(!opt_replace_bool.unwrap(), "Initial value should be false")
    var replaced_bool Option<bool> = opt_replace_bool.replace(true)
    assert(replaced_bool.is_some(), "replace() should return Some(false)")
    assert(!replaced_bool.unwrap(), "replace() should return old value false")
    assert(opt_replace_bool.is_some(), "After replace(), should be Some")
    assert(opt_replace_bool.unwrap(), "After replace(), new value should be true")
    println("✓ replace() with bool type works correctly")

    // Test 96: replace() with negative numbers
    println("\n[Test 96] replace() with negative numbers")
    var opt_replace_neg Option<int> = Option::Some(-50)
    assert(opt_replace_neg.is_some(), "Initial should be Some")
    assert(opt_replace_neg.unwrap() == -50, "Initial value should be -50")
    var replaced_neg Option<int> = opt_replace_neg.replace(-200)
    assert(replaced_neg.is_some(), "replace() should return Some(-50)")
    assert(replaced_neg.unwrap() == -50, "replace() should return old value -50")
    assert(opt_replace_neg.is_some(), "After replace(), should be Some")
    assert(opt_replace_neg.unwrap() == -200, "After replace(), new value should be -200")
    println("✓ replace() with negative numbers works correctly")

    // Test 97: replace() multiple times
    println("\n[Test 97] replace() multiple times")
    var opt_replace_multi Option<int> = Option::Some(1)
    assert(opt_replace_multi.is_some(), "Initial should be Some")
    assert(opt_replace_multi.unwrap() == 1, "Initial value should be 1")
    var replaced_multi1 Option<int> = opt_replace_multi.replace(2)
    assert(replaced_multi1.unwrap() == 1, "First replace() should return 1")
    assert(opt_replace_multi.unwrap() == 2, "After first replace(), should be 2")
    var replaced_multi2 Option<int> = opt_replace_multi.replace(3)
    assert(replaced_multi2.unwrap() == 2, "Second replace() should return 2")
    assert(opt_replace_multi.unwrap() == 3, "After second replace(), should be 3")
    var replaced_multi3 Option<int> = opt_replace_multi.replace(4)
    assert(replaced_multi3.unwrap() == 3, "Third replace() should return 3")
    assert(opt_replace_multi.unwrap() == 4, "After third replace(), should be 4")
    println("✓ replace() multiple times works correctly")

    // Test 98: replace() with zero value
    println("\n[Test 98] replace() with zero value")
    var opt_replace_zero Option<int> = Option::Some(100)
    assert(opt_replace_zero.is_some(), "Initial should be Some")
    assert(opt_replace_zero.unwrap() == 100, "Initial value should be 100")
    var replaced_zero Option<int> = opt_replace_zero.replace(0)
    assert(replaced_zero.is_some(), "replace() should return Some(100)")
    assert(replaced_zero.unwrap() == 100, "replace() should return old value 100")
    assert(opt_replace_zero.is_some(), "After replace(), should be Some")
    assert(opt_replace_zero.unwrap() == 0, "After replace(), new value should be 0")
    println("✓ replace() with zero value works correctly")

    // Test 99: take() then replace()
    println("\n[Test 99] take() then replace()")
    var opt_take_replace Option<int> = Option::Some(77)
    assert(opt_take_replace.is_some(), "Initial should be Some")
    assert(opt_take_replace.unwrap() == 77, "Initial value should be 77")
    var taken_then Option<int> = opt_take_replace.take()
    assert(taken_then.is_some(), "take() should return Some(77)")
    assert(taken_then.unwrap() == 77, "take() should return 77")
    assert(opt_take_replace.is_none(), "After take(), should be None")
    var replaced_then Option<int> = opt_take_replace.replace(88)
    assert(replaced_then.is_none(), "replace() on None should return None")
    assert(opt_take_replace.is_some(), "After replace(), should be Some")
    assert(opt_take_replace.unwrap() == 88, "After replace(), new value should be 88")
    println("✓ take() then replace() works correctly")

    // Test 100: replace() then take()
    println("\n[Test 100] replace() then take()")
    var opt_replace_take Option<int> = Option::Some(99)
    assert(opt_replace_take.is_some(), "Initial should be Some")
    assert(opt_replace_take.unwrap() == 99, "Initial value should be 99")
    var replaced_first Option<int> = opt_replace_take.replace(111)
    assert(replaced_first.is_some(), "replace() should return Some(99)")
    assert(replaced_first.unwrap() == 99, "replace() should return old value 99")
    assert(opt_replace_take.is_some(), "After replace(), should be Some")
    assert(opt_replace_take.unwrap() == 111, "After replace(), new value should be 111")
    var taken_after Option<int> = opt_replace_take.take()
    assert(taken_after.is_some(), "take() should return Some(111)")
    assert(taken_after.unwrap() == 111, "take() should return 111")
    assert(opt_replace_take.is_none(), "After take(), should be None")
    println("✓ replace() then take() works correctly")

    // Test 101: take() with int? syntax
    println("\n[Test 101] take() with int? syntax")
    var opt_take_sugar int? = Some(200)
    assert(opt_take_sugar.is_some(), "Initial should be Some")
    assert(opt_take_sugar.unwrap() == 200, "Initial value should be 200")
    var taken_sugar int? = opt_take_sugar.take()
    assert(taken_sugar.is_some(), "take() should return Some(200)")
    assert(taken_sugar.unwrap() == 200, "take() should return 200")
    assert(opt_take_sugar.is_none(), "After take(), should be None")
    println("✓ take() with int? syntax works correctly")

    // Test 102: replace() with int? syntax
    println("\n[Test 102] replace() with int? syntax")
    var opt_replace_sugar int? = Some(300)
    assert(opt_replace_sugar.is_some(), "Initial should be Some")
    assert(opt_replace_sugar.unwrap() == 300, "Initial value should be 300")
    var replaced_sugar int? = opt_replace_sugar.replace(400)
    assert(replaced_sugar.is_some(), "replace() should return Some(300)")
    assert(replaced_sugar.unwrap() == 300, "replace() should return old value 300")
    assert(opt_replace_sugar.is_some(), "After replace(), should be Some")
    assert(opt_replace_sugar.unwrap() == 400, "After replace(), new value should be 400")
    println("✓ replace() with int? syntax works correctly")

    // Test 103: take() with large numbers
    println("\n[Test 103] take() with large numbers")
    var opt_take_large Option<int> = Option::Some(999999)
    assert(opt_take_large.is_some(), "Initial should be Some")
    assert(opt_take_large.unwrap() == 999999, "Initial value should be 999999")
    var taken_large Option<int> = opt_take_large.take()
    assert(taken_large.is_some(), "take() should return Some(999999)")
    assert(taken_large.unwrap() == 999999, "take() should return 999999")
    assert(opt_take_large.is_none(), "After take(), should be None")
    println("✓ take() with large numbers works correctly")

    // Test 104: replace() with large numbers
    println("\n[Test 104] replace() with large numbers")
    var opt_replace_large Option<int> = Option::Some(500000)
    assert(opt_replace_large.is_some(), "Initial should be Some")
    assert(opt_replace_large.unwrap() == 500000, "Initial value should be 500000")
    var replaced_large Option<int> = opt_replace_large.replace(888888)
    assert(replaced_large.is_some(), "replace() should return Some(500000)")
    assert(replaced_large.unwrap() == 500000, "replace() should return old value 500000")
    assert(opt_replace_large.is_some(), "After replace(), should be Some")
    assert(opt_replace_large.unwrap() == 888888, "After replace(), new value should be 888888")
    println("✓ replace() with large numbers works correctly")


    println("\n==================================================")
    println("✓✓✓ All Option tests passed! ✓✓✓")
    println("==================================================")
    println()
    println("Tested features:")
    println("  - Option<T> construction and methods")
    println("  - Syntactic sugar: T? as Option<T>")
    println("  - Sweeter sugar: Some(v), None (no prefix needed!)")
    println("  - Dart-style nullable: int? a = 10 (auto-wraps to Some(10))")
    println("  - All methods: is_some, is_none, unwrap, map, etc.")
    println("  - Advanced methods: and_then, or_else, filter, and, or, xor")
    println("  - Conversion methods: ok_or, ok_or_else")
    println("  - Map variants: map_or, map_or_else")
    println("  - Mutable methods: take, replace")
    println("  - Negative numbers, bool, large values")
    println("  - Method chaining and complex transformations")
    println("  - Function returns and mixed operations")
}

type ServerInfo struct {
    name String
    load int
}

type ServiceState enum {
    Healthy,
    Warning,
    Down
}

func apply_delta(info &ServerInfo, delta Option<int>) Option<int> {
    var change int = delta?
    var updated int = info.load + change
    if updated < 0 {
        return Option::None()
    }
    info.load = updated
    return Option::Some(updated)
}

func classify_state(code Option<int>) Option<String> {
    var raw int = code?
    var state ServiceState = ServiceState::Down
    if raw == 0 {
        state = ServiceState::Healthy
    } else if raw == 1 {
        state = ServiceState::Warning
    }

    if state == ServiceState::Healthy {
        return Option::Some("healthy")
    }
    if state == ServiceState::Warning {
        return Option::Some("warning")
    }
    return Option::Some("down")
}

func test_option_try_struct_types() {
    println("\n[Struct Test] Applying load adjustments with ?")
    var primary &ServerInfo = &ServerInfo{name: "api-primary", load: 48}
    var updated Option<int> = apply_delta(primary, Option::Some(12))
    assert(updated.is_some(), "Valid delta should succeed")
    assert(updated.unwrap() == 60, "Load should become 60")

    var rollback Option<int> = apply_delta(primary, Option::Some(-70))
    assert(rollback.is_none(), "Negative overload should early-return None")

    var unchanged Option<int> = apply_delta(primary, None)
    assert(unchanged.is_none(), "Missing delta should propagate None")
    println("✓ Struct data works with ?-driven adjustments")
}

func test_option_try_enum_types() {
    println("\n[Enum Test] Translating codes with ?")
    var healthy_label Option<String> = classify_state(Option::Some(0))
    assert(healthy_label.is_some(), "Code 0 should map to healthy label")
    assert(healthy_label.unwrap() == "healthy", "Healthy code should produce healthy label")

    var warning_label Option<String> = classify_state(Option::Some(1))
    assert(warning_label.is_some(), "Code 1 should map to warning label")
    assert(warning_label.unwrap() == "warning", "Warning label expected")

    var invalid_label Option<String> = classify_state(None)
    assert(invalid_label.is_none(), "Missing code should propagate None")
    println("✓ Enum-aware classification works with ?")
}
